package cn.sdormitory.service.impl;

import cn.sdormitory.academic.dao.ClsPracticeInfoDao;
import cn.sdormitory.academic.dao.ClsPracticeResultDao;
import cn.sdormitory.academic.entity.ClsPracticeInfo;
import cn.sdormitory.academic.entity.ClsPracticeResult;
import cn.sdormitory.academic.vo.ClsPracticeInfoQueryVo;
import cn.sdormitory.basedata.dao.BClassDao;
import cn.sdormitory.basedata.entity.BClass;
import cn.sdormitory.common.utils.StringUtils;
import cn.sdormitory.service.AppClsPracticeService;
import cn.sdormitory.sys.dao.SysDictDetailDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysDictDetail;
import cn.sdormitory.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AppClsPracticeServiceImpl extends ServiceImpl<ClsPracticeInfoDao, ClsPracticeInfo> implements AppClsPracticeService {

    @Autowired
    ClsPracticeInfoDao clsPracticeInfoDao;

    @Autowired
    ClsPracticeResultDao clsPracticeResultDao;

    @Autowired
    SysUserDao sysUserDao;

    @Autowired
    BClassDao bClassDao;

    @Autowired
    SysDictDetailDao sysDictDetailDao;


    @Override
    public List<ClsPracticeInfo> findClsPracticeList(ClsPracticeInfoQueryVo vo) {

        //查询当前登录用户部门
        SysUser sysUser = sysUserDao.selectById(vo.getUserId());
        String deptName = sysUser.getDeptName();

        List<ClsPracticeInfo> clsPracticeInfoList = clsPracticeInfoDao.findPageList(0, 10000, vo);

        List<ClsPracticeInfo> returnList = new ArrayList<>();
        for(ClsPracticeInfo item : clsPracticeInfoList){
            if(!"校办".equals(deptName)){
                Long classId = item.getClassId();
                BClass bClass = bClassDao.selectById(classId);
                QueryWrapper<SysDictDetail> wrapper = new QueryWrapper<>();
                wrapper.eq("dict_type","class_dept").eq("dict_value",bClass.getClassDept());
                SysDictDetail sysDictDetail = sysDictDetailDao.selectOne(wrapper);
                if(sysDictDetail.getDictLabel().equals(deptName)){
                    returnList.add(item);
                }
            }else {
                returnList.add(item);
            }
        }
        return returnList;
    }

    @Override
    public List<ClsPracticeResult> findClsPracticeDetails(Long clsPracticeInfoId) {
        LambdaQueryWrapper<ClsPracticeResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClsPracticeResult::getClsPracticeId,clsPracticeInfoId);
        return clsPracticeResultDao.selectList(wrapper);
    }
}
